

import SDKAdatper from "../core/SDKAdatper";
import AdAdapter from "../core/AdAdapter";


/**
 * 快游戏
 */
export default class QuickSDK extends SDKAdatper {
    
    showImage(_tempFilePath: any) {
        this._sdk.previewImage(_tempFilePath)
    }

    createImage(canvas: any, texture?: any) {
        console.log(' createImage ')
        // let texture = this.renderHelper.getTexture()
        let width = texture.width;
        let height = texture.height;
        let bytes = new Uint8Array(width * height * 4);
        texture.readPixels(bytes);
        let data = {
            data: bytes,
            width: canvas.width,
            height: canvas.height,
            fileType: 'png',
            reverse: true,
        }
        return data;
    }

    getFilePath(canvas: any, data: any) {
        let _tempFilePath = this._sdk.saveImageTempSync(data)
        return _tempFilePath;
    }

    saveFile(_tempFilePath: any, show: boolean, call: (r: number) => void) {

        if (this._sdk.saveImageTempSync) {

            console.log('_tempFilePath ', _tempFilePath)
            this._sdk.saveImageToPhotosAlbum({
                filePath: _tempFilePath,
                success: () => {
                    console.log(`save success` + _tempFilePath)
                    if (show) {
                        this.showImage(_tempFilePath)
                    }
                    if (call) {
                        call(1)
                    }
                },
                fail: function (data: any, code: any) {
                    console.log(`handling fail, code = ${code}`)
                    if (call) {
                        call(0)
                    }
                }
            })
        }

    }
    hasInsertAd() {
        if (!this.hasSDK()) {
            return false
        }
        return this._sdk.createInsertAd
    }
    createInterstitialAd(obj: any) {
        if (!this.hasSDK()) {
            return null;
        }

        return new AdAdapter(this._sdk.createInsertAd(obj))
    }

    hasNativeAd() {
        return true
    }
    createNativeAd(obj: any) {
        if (this.hasNativeAd()) {
            return new AdAdapter(this._sdk.createNativeAd(obj))
        }
        return null;
    }

    navigateToMiniProgram(obj: any) {
        if (this.hasSDK()) {
            if (this._sdk.navigateToMiniGame) {
                this._sdk.navigateToMiniGame(obj)
            }
        }
    }

    hasShortcutInstalled(obj: any) {
        if (this.hasSDK() && this._sdk.hasShortcutInstalled) {
            return this._sdk.hasShortcutInstalled(obj)
        }
        return false
    }

    installShortcut(obj: any) {
        if (this.hasSDK() && this._sdk.installShortcut) {
            this._sdk.installShortcut(obj)
        }
    }

    saveImage(obj: any) {
        if (!this.hasSDK() || !this._sdk.saveImageTempSync) {
            return;
        }
        this._sdk.saveImageTempSync(obj)
    }

    loadingComplete(obj: any) {
        if (this.hasSDK() && this._sdk.loadingComplete) {
            this._sdk.loadingComplete(obj)
        }
    }

    setLoadingProgress(obj: any) {
        if (this.hasSDK() && this._sdk.setLoadingProgress) {
            this._sdk.setLoadingProgress(obj)
        }
    }

}